﻿
using System.IO;
using System.Text.RegularExpressions;

namespace CmdPrep.Algorithms
{
    class Replacement : IMySample 
    {    
        public int SDLstart(string filename) {
            string line = null;
             int lineNumber = 0;
             bool found = false;
             
             Regex regexSDL = new Regex("<SDL>");
            StreamReader reader = new StreamReader(filename);
           
            while (((line = reader.ReadLine()) != null) & !found) {
                Match match = regexSDL.Match(line);
                if (match.Length != 0)
                        found = true;
                if (!found)
                     lineNumber++;
            }
            reader.Close();
            if (!found)
                lineNumber =0;
            return ++lineNumber;
        }
        
        public int SDLend(string filename) {
            string line = null;
             int lineNumber = 0;
             bool found = false;
             
             Regex regexSDL = new Regex(@"</SDL>");
            StreamReader reader = new StreamReader(filename);
           
            while (((line = reader.ReadLine()) != null) & !found) {
                Match match = regexSDL.Match(line);
                if (match.Length != 0)
                    found = true;
                else 
                    lineNumber++;
            }
            reader.Close();
            if (!found)
                lineNumber =0;
            return lineNumber;
        }
        
        public string remove_left(string line, string search, string replace_from) {
            string test = null;
            int pos = -1;
            do {
                pos = line.IndexOf(search, pos + 1);
                if (pos >0 ) {            // was: (pos != -1)
                    test = line.Substring(pos - 1, 1);
                    if (test == replace_from) {
                        line = line.Remove(pos - 1, 1);
                        pos -= 2;
                    }
                }
            }
            while (pos != -1);
            return line;
        }
        
        public string remove_right(string line, string search, string replace_from) {
            string test = null;
            int pos = -1;
            do {
                int strLengt= line.Length ;
                pos = line.IndexOf(search, pos + 1);
                if (pos != -1 & pos < line.Length -1) {
                    test = line.Substring(pos + 1, 1);
                    if (test == replace_from) {
                        line = line.Remove(pos + 1, 1);
                        pos -= 1;
                    }
                }
            }
            while (pos != -1);
            return line;
        }
        
        public string remove_end(string line, string replace_from) {
            string test = null;
            int pos = -1;
            do {
                pos = line.Length;
                if (pos > 0 && !string.IsNullOrEmpty(line)) {
                   test = line.Substring(pos-1, 1);
                    if (test == replace_from) {
                        line = line.Remove(pos-1, 1);
                        pos -= 1;
                    }
                }
            }
            while (test == replace_from && !string.IsNullOrEmpty(line));
            return line;
        }
        
        public string add_left(string line, string search, string replacewith, string replace_from) {
            string test = null;
            int test2 = 0;
            int pos = -1;
            do {
                pos = line.IndexOf(search, pos + 1);
                if (pos != -1) {
                    test = line.Substring(pos - 1, 1);
                    if (test != replace_from) {
                        line = line.Insert(pos, replacewith);
                    }
                    test2 = line.Length;
                }
            }
            while (pos != -1);
            return line;
        }
        
        public string add_right(string line, string search, string replacewith, string replace_from) {
            string test = null;
            int test2 = 0;
            int pos = -1;
            do {
                pos = line.IndexOf(search, pos + 1);
                if (pos != -1) {
                    test = line.Substring(pos + 1, 1);
                    if (test != replace_from) {
                        line = line.Insert(pos + 1, replacewith);
                        pos -= 1;
                    }
                    test2 = line.Length;
                }
            }
            while (pos != -1);
            return line;
        }
        
        public string add_end(string line, string replacewith) {
            line = line + replacewith;
            return line;
        }
        
        public string replace(string line, string search, string replacewith) {
            line = line.Replace(search, replacewith);
            return line;
        }
        
        public int FindPos(string line, string search, int placenumber) {
            int x = 0;
            int value = -1;
            int pos = -1;
            do {
                pos = line.IndexOf(search, pos + 1);
                if (pos != -1) {
                    x++;
                    if (x == placenumber) {
                        value = pos;
                    }
                }
            }
            while (pos != -1);
            return value;
        }
    }
}
